package com.moon;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import me.nereo.multi_image_selector.MultiImageSelectorActivity;

import cn.yueliangbaba.R;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.loopj.android.http.RequestParams;
import com.sys.Constants;
import com.sys.DemoApp;
import com.util.ImageLoaderHelper;
import com.util.MgqDataHandler;
import com.util.MgqRestClient;
import com.util.PreferencesUtils;
import com.util.ToastUtil;
import com.util.Utils;
import com.widget.CircleImageView;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

public class DistributeActivity extends BaseActivity implements OnClickListener{
	int mSelectedImg;
	ArrayList<Integer> mClasses = new ArrayList<Integer>();
	boolean mIsParent = true;
	
	List<String> mPhotoInfo = new ArrayList<String>();
	private Bitmap mBitmap[] = new Bitmap[9];
	
	int screenWidth = 0;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_distribute);
		
		WindowManager windowManager = getWindowManager();    
        Display display = windowManager.getDefaultDisplay();    
        screenWidth = display.getWidth();    

		((TextView)findViewById(R.id.btn0)).setSelected(true);
		((TextView)findViewById(R.id.text0)).setSelected(true);
		JSONObject member = JSON.parseObject(PreferencesUtils.getString(this, "member"));

		if(member.getInteger("is_sys") == 0)
		{
			findViewById(R.id.btn1).setVisibility(View.GONE);
			findViewById(R.id.btn2).setVisibility(View.GONE);
			findViewById(R.id.text1).setVisibility(View.GONE);
			findViewById(R.id.text2).setVisibility(View.GONE);
		}
		
		String path = member.getString("avatar_path");
		if(path != null && path.length() > 0) path = "http://120.27.129.233/ylbb/" + path;
		else path = "";
		
		if(setUsername())
			ImageLoaderHelper.displayImage(R.drawable.ic_teacher, (CircleImageView)findViewById(R.id.icon),path);
		else 
			ImageLoaderHelper.displayImage(R.drawable.ic_family, (CircleImageView)findViewById(R.id.icon),path);
		
		((Button)findViewById(R.id.right)).setText("发送");
		((TextView) findViewById(R.id.center)).setText("发布班级圈");
		findViewById(R.id.left).setOnClickListener(this);
		
		findViewById(R.id.btn0).setOnClickListener(this);
		findViewById(R.id.btn1).setOnClickListener(this);
		findViewById(R.id.btn2).setOnClickListener(this);
//		findViewById(R.id.check).setOnClickListener(this);
		findViewById(R.id.class_name).setOnClickListener(this);
		
		findViewById(R.id.img0).setOnClickListener(this);
		findViewById(R.id.img1).setOnClickListener(this);
		findViewById(R.id.img2).setOnClickListener(this);
		findViewById(R.id.img3).setOnClickListener(this);
		findViewById(R.id.img4).setOnClickListener(this);
		findViewById(R.id.img5).setOnClickListener(this);
		findViewById(R.id.img6).setOnClickListener(this);
		findViewById(R.id.img7).setOnClickListener(this);
		findViewById(R.id.img8).setOnClickListener(this);
		findViewById(R.id.right).setOnClickListener(this);
		findViewById(R.id.layout0).setOnClickListener(this);
		findViewById(R.id.layout1).setOnClickListener(this);
		findViewById(R.id.scrollview).setOnTouchListener(new OnTouchListener(){
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				EditText edit=(EditText)findViewById(R.id.content);  
		        InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
		        imm.hideSoftInputFromWindow(edit.getWindowToken(),0);			
		        
		        return v.onTouchEvent(event);
			}
		});
		
		((EditText)findViewById(R.id.content)).addTextChangedListener(new TextWatcher(){

			@Override
			public void afterTextChanged(Editable arg0) {
				((TextView)findViewById(R.id.number)).setText(arg0.length() + "/300");
			}

			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1,
					int arg2, int arg3) {
				
			}

			@Override
			public void onTextChanged(CharSequence arg0, int arg1, int arg2,
					int arg3) {
				
			}
		});
		
		((TextView)findViewById(R.id.name)).setText(member.getString("username"));
		
		JSONArray classes = JSON.parseArray(PreferencesUtils.getString(this, "classes"));
		if(classes.size() == 1)
		{
			JSONObject obj = classes.getJSONObject(0);
			String className = "发送班级: ";
			
			String name = obj.getString("class_name");
			if(obj.getString("class_alias") != null && obj.getString("class_alias").length() > 0) name = obj.getString("class_alias");
			
			className += name;
			((TextView)findViewById(R.id.class_name)).setText(className);
			mClasses.add(obj.getInteger("id"));
		}
	}

	public boolean setUsername()
	{
		JSONObject mMember = JSON.parseObject(PreferencesUtils.getString(this, "member"));
		JSONArray classes = new JSONArray(mMember.getJSONArray("classes"));
		if(classes.size() > 0)
		{
			int count = classes.size();
			for(int i = 0; i < count; i++)
			{
				JSONObject classItem = classes.getJSONObject(i);
				if(classItem.getInteger("is_teacher") > 0) return true;
			}
		}
		return false;
	}
	
	@Override  
    protected void onSaveInstanceState(Bundle outState) {  
        super.onSaveInstanceState(outState);  
        outState.putIntegerArrayList("mClasses", mClasses);  
        if(((TextView)findViewById(R.id.btn0)).isSelected()) 
            outState.putInt("type", 0);  
		if(((TextView)findViewById(R.id.btn1)).isSelected()) 
	        outState.putInt("type", 1);  
		if(((TextView)findViewById(R.id.btn2)).isSelected()) 
	        outState.putInt("type", 2);  
    }  
  
    @Override  
    protected void onRestoreInstanceState(Bundle savedInstanceState) {  
        super.onRestoreInstanceState(savedInstanceState);  

        ((TextView)findViewById(R.id.btn0)).setSelected(false);
        ((TextView)findViewById(R.id.btn1)).setSelected(false);
        ((TextView)findViewById(R.id.btn2)).setSelected(false);
        ((TextView)findViewById(R.id.text0)).setSelected(false);
        ((TextView)findViewById(R.id.text1)).setSelected(false);
        ((TextView)findViewById(R.id.text2)).setSelected(false);
        int type = savedInstanceState.getInt("type");
        if(type == 0) 
        {
        	((TextView)findViewById(R.id.btn0)).setSelected(true);
        	((TextView)findViewById(R.id.text0)).setSelected(true);
        }
        if(type == 1) 
        {
        	((TextView)findViewById(R.id.btn1)).setSelected(true);
        	((TextView)findViewById(R.id.text1)).setSelected(true);
        }
        if(type == 2)
        {
        	((TextView)findViewById(R.id.btn2)).setSelected(true);
        	((TextView)findViewById(R.id.text2)).setSelected(true);
        }

        final JSONArray classes = JSON.parseArray(PreferencesUtils.getString(this, "classes"));
        String className = "发送班级: ";
        
        ArrayList<Integer> classId = savedInstanceState.getIntegerArrayList("mClasses");
        mClasses.removeAll(mClasses);
        mClasses.addAll(classId);
        
		for(int i = 0; i < 9 && classes.size() > i; i++)
		{
			if(className.length() > 6) className += ",";
			for(Integer id : classId)
			{
				if(classes.getJSONObject(i).getInteger("id").intValue() == id.intValue())
				{
					JSONObject item = classes.getJSONObject(i);
					String name = item.getString("class_name");
					if(item.getString("class_alias") != null && item.getString("class_alias").length() > 0) name = item.getString("class_alias");
					className += name;
				}
			}
		}
		((TextView)findViewById(R.id.class_name)).setText(className);
    }  
    
	
	@Override
	public void onClick(View arg0) {
		EditText edit=(EditText)findViewById(R.id.content);  
        InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
        imm.hideSoftInputFromWindow(edit.getWindowToken(),0);
		
		int viewId = arg0.getId();
		switch(viewId)
		{
		case R.id.btn0:
		case R.id.btn1:
		case R.id.btn2:
			((TextView)findViewById(R.id.btn0)).setSelected(false);
			((TextView)findViewById(R.id.btn1)).setSelected(false);
			((TextView)findViewById(R.id.btn2)).setSelected(false);
			((TextView)findViewById(R.id.text0)).setSelected(false);
			((TextView)findViewById(R.id.text1)).setSelected(false);
			((TextView)findViewById(R.id.text2)).setSelected(false);
			if(R.id.btn0 == viewId) 
	        {
	        	((TextView)findViewById(R.id.btn0)).setSelected(true);
	        	((TextView)findViewById(R.id.text0)).setSelected(true);
	        }
	        if(R.id.btn1 == viewId) 
	        {
	        	((TextView)findViewById(R.id.btn1)).setSelected(true);
	        	((TextView)findViewById(R.id.text1)).setSelected(true);
	        }
	        if(R.id.btn2 == viewId)
	        {
	        	((TextView)findViewById(R.id.btn2)).setSelected(true);
	        	((TextView)findViewById(R.id.text2)).setSelected(true);
	        }
			findViewById(R.id.title).setVisibility(View.GONE);
			break;
			
//		case R.id.check:
//			((TextView)arg0).setSelected(!((TextView)arg0).isSelected());
//			break;
			
		case R.id.img0:
		case R.id.img1:
		case R.id.img2:
		case R.id.img3:
		case R.id.img4:	
		case R.id.img5:
		case R.id.img6:
		case R.id.img7:
		case R.id.img8:
			mSelectedImg = viewId - R.id.img0;
			showImagePicker();
			break;
			
		case R.id.class_name:
			showDialog();
			break;
			
		case R.id.right:
			sendRequest();
			break;
			
		case R.id.left:
			finish();
			break;
		}
	}
	
	void showImagePicker()
	{
		Intent intent = new Intent(DistributeActivity.this, MultiImageSelectorActivity.class);
		// whether show camera
		intent.putExtra(MultiImageSelectorActivity.EXTRA_SHOW_CAMERA, true);
		// max select image amount
		intent.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_COUNT, 9);
		// select mode (MultiImageSelectorActivity.MODE_SINGLE OR MultiImageSelectorActivity.MODE_MULTI)
		intent.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_MODE, MultiImageSelectorActivity.MODE_MULTI);
		// default select images (support array list)
		intent.putStringArrayListExtra(MultiImageSelectorActivity.EXTRA_DEFAULT_SELECTED_LIST, (ArrayList<String>) mPhotoInfo);
		startActivityForResult(intent, 11);
		
		return;
		/*
		final Dialog mDialog = new Dialog(this, R.style.Dialog_Fullscreen);
		
		OnClickListener listen = new OnClickListener()
		{
			@Override
			public void onClick(View arg0) {
				if(arg0.getId() == R.id.cancel) mDialog.dismiss();
				else if(arg0.getId() == R.id.pic)
				{
					mDialog.dismiss();
					
					Intent intent = new Intent(DistributeActivity.this, MultiImageSelectorActivity.class);
					// whether show camera
					intent.putExtra(MultiImageSelectorActivity.EXTRA_SHOW_CAMERA, true);
					// max select image amount
					intent.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_COUNT, 9);
					// select mode (MultiImageSelectorActivity.MODE_SINGLE OR MultiImageSelectorActivity.MODE_MULTI)
					intent.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_MODE, MultiImageSelectorActivity.MODE_MULTI);
					// default select images (support array list)
					intent.putStringArrayListExtra(MultiImageSelectorActivity.EXTRA_DEFAULT_SELECTED_LIST, (ArrayList<String>) mPhotoInfo);
					startActivityForResult(intent, 11);
					
//					FunctionConfig config = new FunctionConfig.Builder()
//					    .setMutiSelectMaxSize(9)
//					    .setSelected(mPhotoInfo)
//					    .build();
//					GalleryFinal.openGalleryMuti(12, config, DistributeActivity.this);
				}
				else
				{
					mDialog.dismiss();
					
//					if(mPhotoInfo.size() == 9)
//					{
//						showToast("图片数量已达最大");
//						return;
//					}
//					
//					GalleryFinal.openCamera(11,DistributeActivity.this);
				}
			}
		};
		
		mDialog.setContentView(R.layout.dialog_img_choose);
		mDialog.findViewById(R.id.camera).setOnClickListener(listen);
		mDialog.findViewById(R.id.camera).setVisibility(View.GONE);
		mDialog.findViewById(R.id.pic).setOnClickListener(listen);
		mDialog.findViewById(R.id.cancel).setOnClickListener(listen);
		mDialog.show();*/
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	    super.onActivityResult(requestCode, resultCode, data);
	    if(requestCode == 11){
	        if(resultCode == RESULT_OK && data != null){
	            // Get the result list of select image paths
	            List<String> path = data.getStringArrayListExtra(MultiImageSelectorActivity.EXTRA_RESULT);
	            mPhotoInfo.removeAll(mPhotoInfo);
	            mPhotoInfo.addAll(path);
	            
	            for(int i = 0; i < 9; i++)
	            {
	            	if(mBitmap[i] != null)
	            	{
	            		((ImageView)findViewById(R.id.img0 + i)).setImageBitmap(null);
	            		mBitmap[i].recycle();
	            		mBitmap[i] = null;
	            		System.gc();
	            	}
	            	
	            	if(i < mPhotoInfo.size())
	            	{
	            		findViewById(R.id.img0 + i).setVisibility(View.VISIBLE);
	            		mBitmap[i] = Utils.getImageThumbnail(mPhotoInfo.get(i),160,160);
	            		((ImageView)findViewById(R.id.img0 + i)).setImageBitmap(mBitmap[i]);
	            	}
	            	else if(i == mPhotoInfo.size())
	            	{
	            		findViewById(R.id.img0 + i).setVisibility(View.VISIBLE);
	            		((ImageView)findViewById(R.id.img0 + i)).setImageResource(R.drawable.btn_photo);
	            	}
	            	else
	            	{
	            		findViewById(R.id.img0 + i).setVisibility(View.INVISIBLE);
	            	}
	            }
	        }
	    }
	}
/*
	@Override
	public void onHanlderSuccess(int reqeustCode, List<PhotoInfo> resultList) {
		if(reqeustCode == 11)
		{
			mPhotoInfo.addAll(resultList);
		}
		else
		{
			mPhotoInfo.removeAll(mPhotoInfo);
			mPhotoInfo.addAll(resultList);
		}
		
		 for(int i = 0; i < 9; i++)
         {
         	if(mBitmap[i] != null)
         	{
         		((ImageView)findViewById(R.id.img0 + i)).setImageBitmap(null);
         		mBitmap[i].recycle();
         		mBitmap[i] = null;
         		System.gc();
         	}
         	
         	if(i < mPhotoInfo.size())
         	{
         		findViewById(R.id.img0 + i).setVisibility(View.VISIBLE);
         		mBitmap[i] = Utils.getImageThumbnail(mPhotoInfo.get(i).getPhotoPath(),160,160);
         		((ImageView)findViewById(R.id.img0 + i)).setImageBitmap(mBitmap[i]);
         	}
         	else if(i == mPhotoInfo.size())
         	{
         		findViewById(R.id.img0 + i).setVisibility(View.VISIBLE);
         	}
         	else
         	{
         		findViewById(R.id.img0 + i).setVisibility(View.INVISIBLE);
         	}
         }
		 
		 new Thread(){
			 public void run()
			 {
				 for(int i = 0; i < mPhotoInfo.size(); i++)
				 {
					 getCacheImage(mPhotoInfo.get(i).getPhotoPath(),i);
				 }
			 }
		 }.start();
	}

	@Override
	public void onHanlderFailure(int requestCode, String errorMsg) {
		
	}
*/
	protected void onDestroy()
	{
		super.onDestroy();
		for(int i = 0; i < 9; i++)
		{
			if(mBitmap[i] != null)
			{
				mBitmap[i].recycle();
				System.gc();
				mBitmap[i] = null;
			}
		}
	}
	
	Handler handle = new Handler(){
		public void handleMessage(Message msg)
		{
			if(mClasses.size() == 0)
			{
				ToastUtil.showToastWithAlertPic("请选择发布班级");
				return;
			}
			
			String content = ((TextView)findViewById(R.id.content)).getText().toString();
			if(content.length() == 0)
			{
				ToastUtil.showToastWithAlertPic("标题和内容不能为空");
				return;
			}
			
			MgqDataHandler loginHandler = new MgqDataHandler(DistributeActivity.this, false, false) {
				@Override
				public void onSuccess(String response) {
					JSONObject ruser = JSON.parseObject(response);
					if (ruser.getString("statusCode").equals("0")) {
						try
						{
							if(((TextView)findViewById(R.id.btn0)).isSelected()) MainPageActivity.mMainPageActivity.autoRefresh(0);
							if(((TextView)findViewById(R.id.btn1)).isSelected()) MainPageActivity.mMainPageActivity.autoRefresh(1);
							if(((TextView)findViewById(R.id.btn2)).isSelected()) MainPageActivity.mMainPageActivity.autoRefresh(2);
						}
						catch(Exception e)
						{
							ToastUtil.showToastWithAlertPic(e.getMessage());
						}
						
						finish();
					}
					else 
					{
						ToastUtil.showToastWithAlertPic(ruser.getString("message"));
					}
					
					disMissDailog();
				}
				
				@Override
				public void onFailure(Throwable ble) {
					super.onFailure(ble);
					ToastUtil.showToastWithAlertPic("网络状况不佳，请稍后重试...");
					disMissDailog();
				}
			};
			
			int type = 0;
			
			String classId = "";
			for(Integer id : mClasses)
			{
				if(classId.length() > 0)  classId += ",";
				classId += id;
			}
			
			if(((TextView)findViewById(R.id.btn0)).isSelected()) type = 4;
			if(((TextView)findViewById(R.id.btn1)).isSelected()) type = 1;
			if(((TextView)findViewById(R.id.btn2)).isSelected()) type = 2;
			
			RequestParams params = new RequestParams();
			params.put("id", PreferencesUtils.getString(DistributeActivity.this, "userId"));
			params.put("class_id", classId);
			params.put("title", "");
			params.put("content", content);
			params.put("type", type);
			if(type != 4) params.put("receipt", 1);
			else params.put("receipt", 0);
			
			for(int i = 0; i < mPhotoInfo.size(); i++)
			{
				try {
					File file = new File(Constants.PIC_PATH + i);
					params.put("attach" + (i + 1), file);
				} catch (FileNotFoundException e) {
					e.printStackTrace();
				}
			}
			
			MgqRestClient.post("send_post", params, loginHandler);
		}
	};
	
	private void sendRequest() {
		if(mClasses.size() == 0)
		{
			ToastUtil.showToastWithAlertPic("请选择发布班级");
			return;
		}
		
		String content = ((TextView)findViewById(R.id.content)).getText().toString();
		if(content.length() == 0)
		{
			ToastUtil.showToastWithAlertPic("标题和内容不能为空");
			return;
		}
		
		showWaiting("");
		progressDialog.setCancelable(false);
		
		new Thread(){
  			 public void run()
  			 {
  				 for(int i = 0; i < mPhotoInfo.size(); i++)
  				 {
  					 getCacheImage(mPhotoInfo.get(i),i);
  				 }
  				 
  				handle.sendEmptyMessage(0);
  			 }
  		 }.start();
	}
	
	public String getCacheImage(String filePath,int i){
        OutputStream out = null;
        BitmapFactory.Options option = new BitmapFactory.Options();
        option.inJustDecodeBounds = true;
        Bitmap bitmap = BitmapFactory.decodeFile(filePath, option);
        option.inJustDecodeBounds = false;
        int bWidth = option.outWidth;
        int bHeight= option.outHeight;
        int toWidth = screenWidth;
        int toHeight = screenWidth;
        int be = 1;
        if(bWidth/toWidth>bHeight/toHeight && bWidth>toWidth){
            be = (int)bWidth/toWidth;
            be += 1;
        }else if(bWidth/toWidth<bHeight && bHeight>toHeight){
            be = (int)bHeight/toHeight;
            be += 1;
        }
        option.inSampleSize = be;
        bitmap = BitmapFactory.decodeFile(filePath, option);
        
        String cachePath = Constants.PIC_PATH + i;
        try {
        	File file = new File(cachePath);
        	file.deleteOnExit();
            out = new FileOutputStream(file);
            bitmap.compress(CompressFormat.JPEG, 80, out);
            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        bitmap.recycle();
        bitmap = null;
        System.gc();
        
        return cachePath;
	}
	
	public void showDialog()
	{
		final JSONArray classes = JSON.parseArray(PreferencesUtils.getString(this, "classes"));
		final Dialog mDialog = new Dialog(this, R.style.Dialog_Fullscreen);
		OnClickListener listen = new OnClickListener()
		{
			@Override
			public void onClick(View arg0) {
				if(R.id.dialog_layout == arg0.getId())
				{
					mDialog.dismiss();
				}
				else if(R.id.confirm == arg0.getId())
				{
					mDialog.dismiss();
					String className = "发送班级: ";
					
					for(int i = 0; i < 9 && classes.size() > i; i++)
					{
						if(isClass(classes.getJSONObject(i).getInteger("id")))
						{
							if(className.length() > 6) className += ",";
							if(i == 0) className += ((TextView)mDialog.findViewById(R.id.filter0)).getText().toString();
							if(i == 1) className += ((TextView)mDialog.findViewById(R.id.filter1)).getText().toString();
							if(i == 2) className += ((TextView)mDialog.findViewById(R.id.filter2)).getText().toString();
							if(i == 3) className += ((TextView)mDialog.findViewById(R.id.filter3)).getText().toString();
							if(i == 4) className += ((TextView)mDialog.findViewById(R.id.filter4)).getText().toString();
							if(i == 5) className += ((TextView)mDialog.findViewById(R.id.filter5)).getText().toString();
						}
					}
					
					((TextView)findViewById(R.id.class_name)).setText(className);
				}
				else
				{
					Integer id = (Integer)arg0.getTag();
					if(isClass(id))
					{
						((TextView)arg0).setCompoundDrawablesWithIntrinsicBounds(R.drawable.check_win, 0, 0, 0);
						removeClassId(id);
					}
					else
					{
						((TextView)arg0).setCompoundDrawablesWithIntrinsicBounds(R.drawable.check_win_focus, 0, 0, 0);
						mClasses.add(id);
					}
				}
			}
		};

		mDialog.setContentView(R.layout.dialog_class_center);
		
		int size = classes.size();
		for(int i = 0; i < size; i++)
		{
			JSONObject item = classes.getJSONObject(i);
			
			mDialog.findViewById(R.id.filter0 + i).setOnClickListener(listen);
			mDialog.findViewById(R.id.filter0 + i).setVisibility(View.VISIBLE);
			mDialog.findViewById(R.id.filter0 + i).setTag(item.getInteger("id"));
			if(i == 0) mDialog.findViewById(R.id.line0).setVisibility(View.VISIBLE);
			if(i == 1) mDialog.findViewById(R.id.line1).setVisibility(View.VISIBLE);
			if(i == 2) mDialog.findViewById(R.id.line2).setVisibility(View.VISIBLE);
			if(i == 3) mDialog.findViewById(R.id.line3).setVisibility(View.VISIBLE);
			if(i == 4) mDialog.findViewById(R.id.line4).setVisibility(View.VISIBLE);
			
			if(isClass(item.getInteger("id")))
				((TextView)mDialog.findViewById(R.id.filter0 + i)).setCompoundDrawablesWithIntrinsicBounds(R.drawable.check_win_focus, 0, 0, 0);
			else 
				((TextView)mDialog.findViewById(R.id.filter0 + i)).setCompoundDrawablesWithIntrinsicBounds(R.drawable.check_win, 0, 0, 0);

			String className = item.getString("class_name");
			if(item.getString("class_alias") != null && item.getString("class_alias").length() > 0) className = item.getString("class_alias");
			((TextView)mDialog.findViewById(R.id.filter0 + i)).setText(className);
		}

		mDialog.findViewById(R.id.confirm).setOnClickListener(listen);
		mDialog.findViewById(R.id.dialog_layout).setOnClickListener(listen);
		mDialog.show();
	}
	
	void removeClassId(Integer classId)
	{
		for(Integer id : mClasses)
		{
			if(id.intValue() == classId.intValue())
			{
				mClasses.remove(id);
				break;
			}
		}
	}
	
	boolean isClass(Integer classId)
	{
		
		for(Integer id : mClasses)
		{
			if(id.intValue() == classId.intValue()) 
			{
				return true;
			}
		}
		
		return false;
	}
}
